package com.hdjk.view.network;

import com.hdjk.view.callback.BuyPlanExecuteHistoryCallBack;
import com.hdjk.view.callback.BuyPlanListCallBack;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.callback.CreateBuyPlanCallback;
import com.hdjk.view.config.Constant;
import com.lst.projectlib.entity.Msg;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by sxy on 2018/1/2.
 */

public class PlanNetWork {
    /**
     * 创建基金定投计划
     * @param jsonInfo
     * @param listener
     */
    public static void createPlan(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_CREATE_BUY_PLAN)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new CreateBuyPlanCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取定投计划列表
     *
     * @param listener
     */
    public static void getBuyPlanList(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_GET_BUY_PLAN_LIST)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .addHeader("Authorization", "Bearer " + token)
                    .build()
                    .execute(new BuyPlanListCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
    /**
     * 修改基金定投计划
     * @param jsonInfo
     * @param listener
     */
    public static void modifyPlan(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_MODIFY_BUY_PLAN)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new CreateBuyPlanCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
    /**
     * 暂停基金定投计划
     * @param jsonInfo
     * @param listener
     */
    public static void pausePlan(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_PAUSE_BUY_PLAN)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new CreateBuyPlanCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
    /**
     * 终止基金定投计划
     * @param jsonInfo
     * @param listener
     */
    public static void stopPlan(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_STOP_BUY_PLAN)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new CreateBuyPlanCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
    /**
     * 重启基金定投计划
     * @param jsonInfo
     * @param listener
     */
    public static void restartPlan(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_RESTART_BUY_PLAN)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new CreateBuyPlanCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取定投计划执行情况
     *
     * @param listener
     */
    public static void getBuyPlanExecuteHistory(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_BUY_PLAN_EXECUTE_HISTORY)
                    .addHeader("Authorization", "Bearer " + token)
                    .addParams("paramList",jsonInfo)
                    .build()
                    .execute(new BuyPlanExecuteHistoryCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
}
